Skip to content

Hash passwords with bcrypt by default; upgrade SHA-1 hashes at login - #20

Merged
byjg merged 2 commits into
masterfrom
7.0
Oct 29, 2026
Merged

byjg merged 2 commits into
masterfrom
7.0

Conversation

@byjg

@byjg byjg commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Changes

  • PasswordBcryptMapper is UserModel's default. New and changed passwords are hashed with bcrypt. It also verifies the 40-character SHA-1 hashes of PasswordSha1Mapper, so existing users keep logging in.
  • Upgrade at login: the new UsersService::rehashPasswordIfNeeded() replaces an older hash (SHA-1, or bcrypt with a lower cost) once the password is known; createAuthToken() calls it. isValidUser() only checks the password.
  • Safety net: the new hash is read back; when the password column is too short for it (rejected in strict mode, truncated without it), the old hash is kept and the method returns false, so nobody is locked out before the column is widened.
  • PasswordMapperInterface gains verifyPassword() and needsRehash(); isValidUser() uses them instead of comparing hashes with ===. PasswordSha1Mapper stays selectable and compares with hash_equals().
  • UserModel::setPassword() asks the mapper whether a value is already a hash (it skipped the password definition for any 40-character value).

Breaking changes (see CHANGELOG-7.0.md)

  • Widen the password column to VARCHAR(255).
  • Custom password mappers implement verifyPassword() and needsRehash().
  • UsersServiceInterface has rehashPasswordIfNeeded().

Tests

PasswordBcryptMapperTest and PasswordUpgradeTest (SHA-1 user upgraded at login, wrong password not upgraded, a column that rejects or truncates the hash keeps SHA-1 and the user still logs in, a stored hash not checked against the password definition). Each part of the safety net was checked to fail its test when removed. PHPUnit 182 tests and Psalm pass.

byjg added 2 commits October 29, 2026 18:42
- PasswordBcryptMapper is UserModel's default: a 60-character hash is bcrypt, a 40-character
  one SHA-1, so existing users keep logging in; new and changed passwords get bcrypt
- PasswordMapperInterface: verifyPassword() and needsRehash(); isValidUser() verifies with the
  mapper instead of comparing hashes with ===
- UserModel::setPassword() asks the mapper whether a value is a hash (was strlen != 40)
- PasswordSha1Mapper stays selectable and compares with hash_equals()
- isValidUser() only checks the password
- UsersService::rehashPasswordIfNeeded() replaces an older hash (SHA-1, or bcrypt with a lower
  cost); createAuthToken() calls it at login
- It reads the new hash back and keeps the old one when the column is too short for it
  (rejected or truncated), so nobody is locked out before the column is widened
@byjg
byjg merged commit 0330491 into master Oct 29, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant